#include <sys/types.h>          
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>

/*socket
 * connect
 * send/recv
 * */
#define SERVER_PORT 8888

int main(int argc,char **argv){
    int iSocketClient;
    struct sockaddr_in tSocketClientAddr;
    int iRet;
    unsigned char ucSendBuf[1000];
    int iSendlen;

    if(argc != 2){
        printf("Usage: \n");
        printf("%s server_ip\n", argv[0]);
        return -1;
    }

    iSocketClient = socket(AF_INET, SOCK_DGRAM, 0);
    tSocketClientAddr.sin_family = AF_INET;
    tSocketClientAddr.sin_port = htons(SERVER_PORT);
    if(inet_aton(argv[1],&tSocketClientAddr.sin_addr)==0){
        printf("Server IP Address Error!\n");
        return -1;
    }
    memset(tSocketClientAddr.sin_zero, 0, 8);
    iRet = connect(iSocketClient, (const struct sockaddr*)&tSocketClientAddr,
                   sizeof(struct sockaddr));//实际上并不会真正建立连接
    if(iRet == -1){
        printf("connect error!\n");
        return -1;
    }

    while(1){
        if(fgets(ucSendBuf, 999,stdin)){
            iSendlen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0);
            if(iSendlen <= 0){
                close(iSocketClient);
                return -1;
            }
        }
    }

    return 0;
}